#include "../../common/etcd.hpp"
#include <gflags/gflags.h>
#include <brpc/server.h>
#include <butil/logging.h>
#include "main.pb.h"

DEFINE_bool(run_mode, false, "程序运行模式 --false: 调试, --true: 发布");
DEFINE_string(log_file, "./log.txt", "发布模式下, 指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 指定日志的输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控的根目录");
DEFINE_string(instance_name, "/echo/instance", "当前实例名称");
DEFINE_string(access_host, "127.0.0.1:11000", "当前外部访问地址");
DEFINE_int32(listen_port, 11000, "RPC服务器监听端口");

class EchoServiceImpl : public example::EchoService
{
public:
    void Echo(google::protobuf::RpcController *controller, // 用于控制 RPC 调用和获取状态信息
              const ::example::EchoRequest *request,       // 客户端发送的请求消息
              ::example::EchoResponse *response,           // 需要填充的响应消息
              ::google::protobuf::Closure *done)           // 回调对象，主要应用于异步请求，处理完成后需要调用它
    {
        brpc::ClosureGuard rpc_guard(done);
        std::cout << "收到消息: " << request->message() << std::endl;

        std::string str = request->message() + "这是一个响应！！";
        response->set_message(str);
    }
};

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 关闭brpc的默认日志输出
    logging::LoggingSettings settings;
    settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(settings);

    // 构造服务器对象
    brpc::Server server;
    // 向服务器对象中，新增EchoService服务
    EchoServiceImpl echo_service;
    int ret = server.AddService(&echo_service, brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);
    if (ret == -1)
    {
        std::cout << "添加Rpc服务失败!\n";
        return -1;
    }
    // 启动服务器
    brpc::ServerOptions options;
    options.idle_timeout_sec = -1; // 连接空闲超时时间-超时后连接被关闭   -1表示禁用连接空闲超时
    options.num_threads = 1;       // io线程数量
    ret = server.Start(FLAGS_listen_port, &options);
    if (ret == -1)
    {
        std::cout << "启动服务器失败!\n";
        return -1;
    }
    // 注册服务
    Registry::ptr rclient = std::make_shared<Registry>(FLAGS_etcd_host);
    rclient->registry(FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);

    server.RunUntilAskedToQuit(); // 休眠等待服务结束运行
    return 0;
}